using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OurWedding1.Models;

namespace OurWedding1.Controllers
{
    [Authorize]
    public class PlacesController : BaseController
    {
		private readonly IWeddingRepository weddingRepository;
		private readonly IPlaceTypeRepository placetypeRepository;
		private readonly IPlaceRepository placeRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public PlacesController() : this(new WeddingRepository(), new PlaceTypeRepository(), new PlaceRepository())
        {
        }

        public PlacesController(IWeddingRepository weddingRepository, IPlaceTypeRepository placetypeRepository, IPlaceRepository placeRepository)
        {
			this.weddingRepository = weddingRepository;
			this.placetypeRepository = placetypeRepository;
			this.placeRepository = placeRepository;
        }

        //
        // GET: /Places/

        public ViewResult Index()
        {
            return View(placeRepository.AllIncluding(place => place.Wedding, place => place.PlaceType));
        }

        //
        // GET: /Places/Details/5

        public ViewResult Details(int id)
        {
            return View(placeRepository.Find(id));
        }

        //
        // GET: /Places/Create

        public ActionResult Create()
        {
			ViewBag.PossibleWeddings = weddingRepository.All;
			ViewBag.PossiblePlaceTypes = placetypeRepository.All;
            return View();
        } 

        //
        // POST: /Places/Create

        [HttpPost]
        public ActionResult Create(Place place)
        {
            if (ModelState.IsValid) {
                placeRepository.InsertOrUpdate(place);
                placeRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleWeddings = weddingRepository.All;
				ViewBag.PossiblePlaceTypes = placetypeRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Places/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleWeddings = weddingRepository.All;
			ViewBag.PossiblePlaceTypes = placetypeRepository.All;
             return View(placeRepository.Find(id));
        }

        //
        // POST: /Places/Edit/5

        [HttpPost]
        public ActionResult Edit(Place place)
        {
            if (ModelState.IsValid) {
                placeRepository.InsertOrUpdate(place);
                placeRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleWeddings = weddingRepository.All;
				ViewBag.PossiblePlaceTypes = placetypeRepository.All;
				return View();
			}
        }

        //
        // GET: /Places/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(placeRepository.Find(id));
        }

        //
        // POST: /Places/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            placeRepository.Delete(id);
            placeRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                weddingRepository.Dispose();
                placetypeRepository.Dispose();
                placeRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

